<?php

/* 
 * To change this license header, choose License Headers in Project Properties.
 * To chanjdbc:postgresql://41.191.241.162:5432/esoko_20131128 [busylab on public]ge this template file, choose Tools | Templates
 * and open the template in the editor.
 */


/*
 * creates or update contact for a users device
 * 
 * @param args = {
 * 
 *  @param id
 *  @param name
 *  @param email
 *  @param msisdn
 *  @param user_id
 *  @param device_id
 *  @param date
 *  @param status
 * 
 * } 
 * 
 */
function contacts_save($args) {
    
    //import required global variables 
    global $db;
    global $contex;

    //extract and sanitize parameters creating a variable for each
    $fields = X_param_array ($args, 'fields'); 
    $id = X_param_array ($args, 'id'); 
    $data['name'] = X_param_array ($args, 'name'); if(!$id && !$data['name']) return __MOOV_PARAM_INVAL_;
    $data['email'] = X_param_array ($args, 'email'); 
    $data['msisdn'] = X_param_array ($args, 'msisdn'); 
    $data['country'] = X_param_array ($args, 'country'); 
    $data['added_ts'] = X_param ($args, 'added_ts', gmdate('y-m-d h:m:s')); 
    $data['modified_ts'] = X_param ($args, 'modified_ts', gmdate('y-m-d h:m:s'));
    $data['status'] = X_param_array ($args, 'status'); 
    $limit = X_param_array ($args, 'limit');
    $sort = X_param_array ($args, 'sort');
    if(!$data['msisdn'] && !$data['email']) return __MOOV_PARAM_INVAL_;
    
    // ---------------- Operation begins here ---------------------
    $id = $db->save('contacts',$id,$data);
    return contacts_list(array('id' => $id, 'fields' => $fields, 'limit' => $limit, 'sort' => $sort));
  
}

/*
 * list contact for a users device
 * 
 * @param args = {
 * 
 *  @param id
 *  @param name
 *  @param email
 *  @param msisdn
 *  @param user_id
 *  @param device_id
 *  @param date
 *  @param status
 * 
 * } 
 * 
 */
function contacts_list($args) {

    //import required global variables 
    global $db;
    global $contex;
    
    $fields = X_param_array ($args, 'fields'); 
    $data['id'] = X_param_array ($args, 'id'); 
    $data['name'] = X_param_array ($args, 'name'); 
    $data['email'] = X_param_array ($args, 'email'); 
    $data['msisdn'] = X_param_array ($args, 'msisdn'); 
    $data['country'] = X_param_array ($args, 'country'); 
    $data['user_id'] = X_param_array ($args, 'user_id');
    $data['device_id'] = X_param_array ($args, 'device_id');
    $data['added_ts'] = X_param ($args, 'added_ts'); 
    $data['modified_ts'] = X_param ($args, 'modified_ts');
    $data['status'] = X_param_array ($args, 'status', 'A'); 
    $func_fields = X_param($args, 'func_fields','X');
    $limit = X_param_array ($args, 'limit');
    $sort = X_param_array ($args, 'sort');
    
    // ---------------- Operation begins here ---------------------

        if($func_fields === 'X'){
            //include custom queries
            if(in_array('device_id', $fields)) X_field ($fields, 'select alias from devices where id = contacts.device_id', 'device_name');
        }        
        $rc = $db->get('contacts', $fields, $data, $limit, $sort);
     return out($rc); 
}

